#!/usr/bin/env python

import os
import cgi
from google.appengine.ext import webapp
from google.appengine.ext.webapp.util import run_wsgi_app
from google.appengine.ext.webapp import template

# add our modules to syspath
import sys
sys.path.insert(0, "modules")

from models import TheZipFile

class Build(webapp.RequestHandler):
	zipfile = TheZipFile()
	root = os.path.dirname(__file__)
	def get(self):
		self.MakeBuild()
		template_values = {
			'TITLE': lang_header,
			'LANG': lang_lang,
			'LANGDOWN': lang_download,
			'NUM': '001', # TODO: create counter
			'LANGSIZE': lang_size,
			'SIZE':	"0kb",
			'MAKEAGAIN': lang_make_again,
		    'USER_LANG': 'ru', # TODO: load locales
		}
		path = os.path.join(root, 'templates/full/done.tpl')    
		self.response.out.write(template.render(path, template_values))

	def MakeBuild(self):
		global zipfile
		# select core files dir
		val = cgi.escape(self.request.get("type"))
		buildtype = "fullbuild" if (val == "") else val
#		val = cgi.escape(self.request.get("mod_name"))
#		zip.name = "BombusMod"  if (val == "") else val
		rootdir = os.path.join(self.root, os.path.join("distfiles", buildtype))
		# select resources
		resources = { 
		"images": "standard",
		"smiles": "bigTasha",
		"icons": "svg",
		"locales": "enru",
		"skins": "standard",
		"sound": "standard",
		}
		resdirs = []
		for key in resources.keys():
			val = cgi.escape(self.request.get(key))
			if (val <> ""):
				resources.keys[key] = val
			resdirs.append(os.path.join(os.path.dirname(__file__), os.path.join("resources", resources[key])))
		# create jar and store
		zipfile.Make(rootdir, resdirs)
		



application = webapp.WSGIApplication([('/Build', Build)],debug=True)
locale_file = os.path.join(os.path.dirname(__file__), 'templates/locales/ru/Main.txt.py')
execfile(locale_file)
def main():  
	run_wsgi_app(application)

if __name__ == "__main__":
	main()